/*
 * @(#)MobileCheckFilter.java $version 2011. 10. 2.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.luffy.interceptor;

import java.util.HashSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.luffy.mobile.browser.util.MobileBrowserUtil;
import com.luffy.mobile.browser.util.MobileBrowserUtil.MobileBrowserType;

/**
 * @author Administrator
 */
public class MobileCheckInterceptor extends HandlerInterceptorAdapter{
	private String mobileViewPrefix = "mobile";
	private String requestParamName = "targetService";
	private String requestParamValue = "mobile";
	
	private HashSet<String> mobileCheckWords = new HashSet<String>();
	
	private static final String[] DEFAULT_WORDS = {"mobile","iphone","android","ipod","lgtelecom","ppc","symbianos","blackberry"};
	
	public MobileCheckInterceptor(){
		createDefaultWords();
	}
	public void postHandle(
			HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
			throws Exception {
		if(isMobileRequest(request)){
			request.setAttribute("browserType", getBrowserType(request));
			modelAndView.setViewName(mobileViewPrefix +"/"+ modelAndView.getViewName());
		}
	}

	/**
	 * @param request 
	 * @return
	 */
	private String getBrowserType(HttpServletRequest request) {
		String ua = request.getHeader("user-agent");
		String sktua = request.getHeader("X-SKT-User-Agent");

		MobileBrowserType browserType = MobileBrowserUtil.getBrowserType(ua, sktua);
		return browserType.getBrowserName();
	}
	/**
	 * @param request
	 * @return
	 */
	private boolean isMobileRequest(HttpServletRequest request) {
		String requestUrl = request.getParameter(getRequestParamName());
		if(requestUrl != null && requestUrl.toLowerCase().equals(getRequestParamValue().toLowerCase())){
			return true;
		}
		
		String userAgent = request.getHeader("User-Agent").toLowerCase();
		for (String word : mobileCheckWords) {
			if(userAgent.indexOf(word) > -1){
				return true;
			}
		}
		return false;
	}
	
	public void init() throws ServletException {
		createDefaultWords();
	}
	/**
	 * 
	 */
	private void createDefaultWords() {
		mobileCheckWords.clear();
		for (String word : DEFAULT_WORDS) {
			mobileCheckWords.add(word);			
		}
	}
	public void setMobileViewPrefix(String mobileViewPrefix) {
		this.mobileViewPrefix = mobileViewPrefix;
	}
	public String getMobileViewPrefix() {
		return mobileViewPrefix;
	}
	public void setRequestParamName(String requestParamName) {
		this.requestParamName = requestParamName;
	}
	public String getRequestParamName() {
		return requestParamName;
	}
	public void setRequestParamValue(String requestParamValue) {
		this.requestParamValue = requestParamValue;
	}
	public String getRequestParamValue() {
		return requestParamValue;
	}
}
